package com.zhang.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhang.service.AlbumService;
import com.zhang.util.Log;
import com.zhang.view_object.UserVO;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * @author happy
 **/
@WebServlet("/studio/album/bind.json")
public class BindJsonServlet extends HttpServlet {
    private final AlbumService albumService = new AlbumService();
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    @SneakyThrows
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        UserVO currentUser = null;
        HttpSession session = req.getSession(false);
        if (session != null) {
            currentUser = (UserVO) session.getAttribute("currentUser");
        }

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        PrintWriter writer = resp.getWriter();
        if (currentUser == null) {
            Log.println("UploadDoServlet用户未登录");
            writer.println("该功能需要登录后才能使用");
            return;
        }

        req.setCharacterEncoding("utf-8");
        int aid = Integer.parseInt(req.getParameter("aid"));

        Object view = albumService.getBindInfo(currentUser,aid);

        String json = objectMapper.writeValueAsString(view);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);

    }
}